package chooser;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChooseMenu extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 9081606331653514325L;

	public static ChooseMenu _instance;

	private JButton[] btn;
	private GameFrame[] frame;
	
	private JPanel panel;
	
	private ChooseMenu()
	{
		/*
		 * Set frame parameters
		 *
		 */
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);		
		
		/*
		 * Generate Buttons and associate them with demos
		 *
		 */
		btn = new JButton[3];
		frame = new GameFrame[3];
		frame[0] = new mousemove.GameStart(0);
		frame[1] = new mousecollide.GameStart(0);
		frame[2] = new console.GameStart(0);
		
		for (int i  =0; i < btn.length; i++)
		{
			btn[i] = new JButton(frame[i].getClass().getPackage().getName());
			btn[i].addActionListener(this);
			btn[i].setActionCommand(btn[i].getText());
		}
		
		/*
		 * Put buttons on the panel
		 *
		 */
		Container c = this.getContentPane();
		panel = new JPanel();
		for (int i = 0; i < btn.length; i++)
		{
			panel.add(btn[i]);
		}
		c.add(panel);
		
		/*
		 * Add Action Listener for Buttons
		 *
		 */
		
		this.pack();
		centralise();
	}
	
	private void centralise()
	{
		Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((screen_size.width - this.getWidth())>>1, 
		    		(screen_size.height - this.getHeight())>>1);
	}
	
	public static ChooseMenu getInstance()
	{
		if (_instance == null)
		{
			_instance = new ChooseMenu();
		}
		return _instance;
	}

	public void actionPerformed(ActionEvent e)
	{
		for (int i = 0; i < btn.length; i++)
		{
			if (e.getActionCommand() == btn[i].getText())
			{
				this.setVisible(false);
				frame[i].showon();
				break;
			}
		}
	}
}
